package com.aptemo.webetek.client.widgets.scoreboard;

import java.util.ArrayList;
import java.util.List;

import com.aptemo.webetek.client.WebetekUserServiceAsync;
import com.aptemo.webetek.client.commands.GotAsynchCallback;
import com.aptemo.webetek.client.commands.impl.GetPlayersPagingAction;
import com.aptemo.webetek.client.enums.EnumUserOrdering;
import com.aptemo.webetek.client.enums.EnumUserStatus;
import com.aptemo.webetek.client.exceptions.BackendException.EErrorCodes;
import com.aptemo.webetek.client.widgets.paging.PagingPresenter;
import com.aptemo.webetek.shared.PagingResult;
import com.aptemo.webetek.shared.UserBean;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.ui.HasWidgets;

public class TeamStandingsPagingPresenter
		extends PagingPresenter implements ClickHandler
{

	private HandlerManager			meventBus;
	private WebetekUserServiceAsync	mservice;
	private TeamStandingsDisplay	display;
	private int						pageNo				= 0;
	private String					teamId;

	private List<UserBean>			list				= new ArrayList<UserBean>();

	private EnumUserOrdering		order				= EnumUserOrdering.WON_DESC;

	private EnumUserOrdering		lastWonOrder		= EnumUserOrdering.WON_DESC;
	private EnumUserOrdering		lastEarningsOrder	= EnumUserOrdering.EARNINGS_ASC;
	private EnumUserOrdering		lastBalanceOrder	= EnumUserOrdering.BALANCE_ASC;
	private EnumUserOrdering		lastTotalOrder		= EnumUserOrdering.TOTAL_ASC;
	private UserBean				currentUser;

	public TeamStandingsPagingPresenter(TeamStandingsDisplay view, HandlerManager eventbus, WebetekUserServiceAsync rpc, String id)
	{
		this(view, eventbus, rpc, id, 0);
	}

	public TeamStandingsPagingPresenter(TeamStandingsDisplay view, HandlerManager eventBus, WebetekUserServiceAsync rpcService, String teamid, int pageNo)
	{
		super(view.getPaging(), 10);

		this.meventBus = eventBus;
		this.mservice = rpcService;
		this.display = view;

		init(currentUser, teamid, "", pageNo);

	}

	private void bindDisplay()
	{

		display.getEarningsLink().addClickHandler(this);
		display.getPlacedLink().addClickHandler(this);
		display.getWonLink().addClickHandler(this);

		display.getList().addClickHandler(new ClickHandler()
		{

			@Override
			public void onClick(ClickEvent event)
			{
				int selectedRow = display.getClickedRow(event);

				if (selectedRow >= 0)
				{
					// get it from paging...
					// UserBean bet = getItem(selectedRow);
					// String id = bet.getKey();
					// meventBus.fireEvent(new SelectedBetEvent(id, teamId));
				}

			}
		});

	}

	private void retrieveList(final int page)
	{
		final String destPage = getCursor(page);

		display.getInfoField().setText(display.translate("loading"));

		GetPlayersPagingAction action = new GetPlayersPagingAction(this.teamId, EnumUserStatus.ACTIVE, order, destPage, getPageSize());

		mservice.invoke(action, new GotAsynchCallback<PagingResult<UserBean>>()
		{
			@Override
			public void onSuccess(PagingResult<UserBean> result)
			{
				// paging staff...
				if (result.getTotalCnt() >= 0)
				{
					setPagingTotalSize(result.getTotalCnt());
				}
				pageNo = updatePaging(page, result.getCursor());

				// save list
				list = result.getList();
				;
				display.getInfoField().setText("");
				display.renderList(list);
			}

			@Override
			public void onError(String message, EErrorCodes code)
			{
				display.getInfoField().setText(message);
			}
		});
	}

	@Override
	public void go(HasWidgets container)
	{
		bindDisplay();

		container.clear();
		container.add(display.getDisplay());

		retrieveList(pageNo);
	}

	@Override
	public void onClick(ClickEvent event)
	{
		if (event.getSource() == display.getEarningsLink())
		{
			order = lastEarningsOrder == EnumUserOrdering.EARNINGS_ASC ? EnumUserOrdering.EARNINGS_DESC : EnumUserOrdering.EARNINGS_ASC;
			lastEarningsOrder = order;
			// earningsComparator.setFactor(earningsComparator.getFactor() *
			// -1);
			// currentComparator = earningsComparator;
			// retrieveList();
		}
		else if (event.getSource() == display.getPlacedLink())
		{
			order = lastTotalOrder == EnumUserOrdering.TOTAL_ASC ? EnumUserOrdering.TOTAL_DESC : EnumUserOrdering.TOTAL_ASC;
			lastTotalOrder = order;

			// placedComparator.setFactor(placedComparator.getFactor() * -1);
			// currentComparator = placedComparator;
			// retrieveList();

		}
		else if (event.getSource() == display.getWonLink())
		{
			order = lastWonOrder == EnumUserOrdering.WON_ASC ? EnumUserOrdering.WON_DESC : EnumUserOrdering.WON_ASC;
			lastWonOrder = order;
			// wonComparator.setFactor(wonComparator.getFactor() * -1);
			// currentComparator = wonComparator;
			// retrieveList();
		}
		else
		{
			return;
		}
		// sort it
		// Collections.sort(list, currentComparator);
		// renderList(list);
		retrieveList(pageNo);
	}

	private void init(UserBean currentUser, String teamid, String filter, int pageNo)
	{
		this.teamId = teamid;
		this.pageNo = pageNo;
		this.currentUser = currentUser;
	}

	public void update(UserBean currentUser, String teamId, String filter, int pageNo)
	{
		init(currentUser, teamId, filter, pageNo);

		retrieveList(pageNo);
	}

}
